#!/usr/make
#
# Makefile for SQLITE FTS extensions, use in conjunction with
# mingw-cross-build.sh

#### The toplevel directory of the source tree.  This is the directory
#    that contains this "Makefile.in" and the "configure.in" script.
#
TOP = ../sqlite3

#### C Compiler and options for use in building executables that
#    will run on the platform that is doing the build.
#
BCC = gcc -g -O2
#BCC = /opt/ancic/bin/c89 -0

#### If the target operating system supports the "usleep()" system
#    call, then define the HAVE_USLEEP macro for all C modules.
#
USLEEP = 
#USLEEP = -DHAVE_USLEEP=1

#### If you want the SQLite library to be safe for use within a 
#    multi-threaded program, then define the following macro
#    appropriately:
#
THREADSAFE = -DSQLITE_THREADSAFE=1
#THREADSAFE = -DSQLITE_THREADSAFE=0

#### Specify any extra linker options needed to make the library
#    thread safe
#
#THREADLIB = -lpthread
THREADLIB = 

#### Specify any extra libraries needed to access required functions.
#
#TLIBS = -lrt    # fdatasync on Solaris 8
TLIBS = 

#### Leave SQLITE_DEBUG undefined for maximum speed.  Use SQLITE_DEBUG=1
#    to check for memory leaks.  Use SQLITE_DEBUG=2 to print a log of all
#    malloc()s and free()s in order to track down memory leaks.
#    
#    SQLite uses some expensive assert() statements in the inner loop.
#    You can make the library go almost twice as fast if you compile
#    with -DNDEBUG=1
#
#OPTS = -DSQLITE_DEBUG=2
#OPTS = -DSQLITE_DEBUG=1
#OPTS = 
OPTS =	-DNDEBUG=1 -DSQLITE_ENABLE_COLUMN_METADATA=1 -DSQLITE_ENABLE_FTS3=1 \
	-DSQLITE_ENABLE_MEMSYS5=1 \
	-DWIN32=1 -DSQLITE_OS_WIN=1 $(THREADSAFE)
#OPTS += -DHAVE_FDATASYNC=1

#### The suffix to add to executable files.  ".exe" for windows.
#    Nothing for unix.
#
EXE = .exe
#EXE =

#### C Compile and options for use in building executables that 
#    will run on the target platform.  This is usually the same
#    as BCC, unless you are cross-compiling.
#
#TCC = gcc -O6
#TCC = gcc -g -O0 -Wall
#TCC = gcc -g -O0 -Wall -fprofile-arcs -ftest-coverage
TCC = /opt/mingw64/bin/x86_64-w64-mingw32-gcc -m32 -march=i386 -mtune=i386 \
      -O2 -DNO_TCL
#TCC = /opt/ansic/bin/c89 -O +z -Wl,-a,archive

#### Tools used to build a static library.
#
#AR = ar cr
AR = /opt/mingw64/bin/x86_64-w64-mingw32-ar cr
#RANLIB = ranlib
RANLIB = /opt/mingw64/bin/x86_64-w64-mingw32-ranlib

# MKSHLIB = gcc -shared
# SO = so
# SHPREFIX = lib
MKSHLIB = /opt/mingw64/bin/x86_64-w64-mingw32-ar -shared
SO = dll
SHPREFIX =

#### Extra compiler options needed for programs that use the TCL library.
#
TCL_FLAGS =
#TCL_FLAGS = -DSTATIC_BUILD=1
#TCL_FLAGS = -I/home/drh/tcltk/8.4linux
#TCL_FLAGS = -I/home/drh/tcltk/8.4win -DSTATIC_BUILD=1
#TCL_FLAGS = -I/home/drh/tcltk/8.3hpux

#### Linker options needed to link against the TCL library.
#
LIBTCL =
#LIBTCL = -ltcl -lm -ldl
#LIBTCL = /home/drh/tcltk/8.4linux/libtcl8.4g.a -lm -ldl
#LIBTCL = /home/drh/tcltk/8.4win/libtcl84s.a -lmsvcrt
#LIBTCL = /home/drh/tcltk/8.3hpux/libtcl8.3.a -ldld -lm -lc

#### Compiler options needed for programs that use the readline() library.
#
READLINE_FLAGS =
#READLINE_FLAGS = -DHAVE_READLINE=1 -I/usr/include/readline

#### Linker options needed by programs using readline() must link against.
#
LIBREADLINE =
#LIBREADLINE = -static -lreadline -ltermcap

#### Which "awk" program provides nawk compatibilty
#
# NAWK = nawk
NAWK = awk

# This is how we compile
#
TCCX = $(TCC) $(OPTS) $(THREADSAFE) $(USLEEP) -I. -I$(TOP) -I$(TOP)/src

ifeq ($(MSVCRT),70)
	TCCX   += -D__MSVCRT_VERSION=0x0700
	LMSVCRT = -nodefaultlibs -lmingw32 -lgcc_eh -lmoldname -lmingwex -lgcc -lmsvcr70
endif
ifeq ($(MSVCRT),80)
	TCCX   += -D__MSVCRT_VERSION=0x0800
	LMSVCRT = -nodefaultlibs -lmingw32 -lgcc_eh -lmoldname -lmingwex -lgcc -lmsvcr80
endif
ifeq ($(MSVCRT),90)
	TCCX   += -D__MSVCRT_VERSION=0x0900
	LMSVCRT = -nodefaultlibs -lmingw32 -lgcc_eh -lmoldname -lmingwex -lgcc -lmsvcr90 -lmsvcrt
endif
ifeq ($(MSVCRT),100)
	TCCX   += -D__MSVCRT_VERSION=0x0A00
	LMSVCRT = -nodefaultlibs -lmingw32 -lgcc_eh -lmoldname -lmingwex -lgcc -lmsvcr100 -lmsvcrt
endif
ifeq ($(LMSVCRT),)
	TCCX   += -D_USE_32BIT_TIME_T=1
	LMSVCRT = -lmsvcrt
endif

# Object files for FTS1
#
FTS1OBJ+= fts1.o fts1_hash.o fts1_porter.o fts1_tokenizer1.o

# Object files for FTS2
#
FTS2OBJ+= fts2.o fts2_hash.o fts2_porter.o fts2_tokenizer1.o

# Object files for FTS3
#
FTS3OBJ+= fts3.o fts3_hash.o fts3_porter.o fts3_tokenizer.o fts3_tokenizer1.o \
	fts3_expr.o fts3_icu.o fts3_snippet.o fts3_write.o fts3_aux.o \
	fts3_tokenize_vtab.o fts3_unicode.o fts3_unicode2.o

# FTS1 source code files.
#
FTS1SRC += \
  $(TOP)/ext/fts1/fts1.c \
  $(TOP)/ext/fts1/fts1.h \
  $(TOP)/ext/fts1/fts1_hash.c \
  $(TOP)/ext/fts1/fts1_hash.h \
  $(TOP)/ext/fts1/fts1_porter.c \
  $(TOP)/ext/fts1/fts1_tokenizer.h \
  $(TOP)/ext/fts1/fts1_tokenizer1.c

# FTS2 source code files.
#
FTS2SRC += \
  $(TOP)/ext/fts2/fts2.c \
  $(TOP)/ext/fts2/fts2.h \
  $(TOP)/ext/fts2/fts2_hash.c \
  $(TOP)/ext/fts2/fts2_hash.h \
  $(TOP)/ext/fts2/fts2_porter.c \
  $(TOP)/ext/fts2/fts2_tokenizer.h \
  $(TOP)/ext/fts2/fts2_tokenizer1.c

# FTS3 source code files.
#
FTS3SRC += \
  $(TOP)/ext/fts3/fts3.c \
  $(TOP)/ext/fts3/fts3.h \
  $(TOP)/ext/fts3/fts3_hash.c \
  $(TOP)/ext/fts3/fts3_hash.h \
  $(TOP)/ext/fts3/fts3_porter.c \
  $(TOP)/ext/fts3/fts3_tokenizer.h \
  $(TOP)/ext/fts3/fts3_tokenizer1.c \
  $(TOP)/ext/fts3/fts3_expr.c \
  $(TOP)/ext/fts3/fts3_icu.c \
  $(TOP)/ext/fts3/fts3_snippet.c \
  $(TOP)/ext/fts3/fts3_write.c \
  $(TOP)/ext/fts3/fts3_aux.c \
  $(TOP)/ext/fts3/fts3_tokenize_vtab.c \
  $(TOP)/ext/fts3/fts3_unicode.c \
  $(TOP)/ext/fts3/fts3_unicode2.c

# Common headers
HDR += $(TOP)/sqlite3.h \
  $(TOP)/src/sqlite3ext.h

# Header files used by FTS1
#
FTS1HDR += \
  $(TOP)/ext/fts1/fts1.h \
  $(TOP)/ext/fts1/fts1_hash.h \
  $(TOP)/ext/fts1/fts1_tokenizer.h

# Header files used by FTS2
#
FTS1HDR += \
  $(TOP)/ext/fts2/fts2.h \
  $(TOP)/ext/fts2/fts2_hash.h \
  $(TOP)/ext/fts2/fts2_tokenizer.h

# Header files used by FTS3
#
FTS3HDR += \
  $(TOP)/ext/fts3/fts3.h \
  $(TOP)/ext/fts3/fts3_hash.h \
  $(TOP)/ext/fts3/fts3_tokenizer.h \
  $(TOP)/ext/fts3/fts3Int.h

# This is the default Makefile target.  The objects listed here
# are what get build when you type just "make" with no arguments.
#
# SQLite 3.4.*
#all:	sqlite3_mod_fts1.$(SO) sqlite3_mod_fts2.$(SO)
# SQLite 3.5.*
all:	sqlite3_mod_fts3.$(SO)


# Rules to build individual files
#
fts1.o:	$(TOP)/ext/fts1/fts1.c $(FTS1HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts1/fts1.c

fts1_hash.o:	$(TOP)/ext/fts1/fts1_hash.c $(FTS1HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts1/fts1_hash.c

fts1_porter.o:	$(TOP)/ext/fts1/fts1_porter.c $(FTS1HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts1/fts1_porter.c

fts1_tokenizer1.o:	$(TOP)/ext/fts1/fts1_tokenizer1.c $(FTS1HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts1/fts1_tokenizer1.c

sqlite3_mod_fts1.$(SO):	$(FTS1OBJ)
	$(TCCX) -shared -Wl,--kill-at \
	    -Wl,-out-implib,libsqlite3fts1.a -Wl,--strip-all \
	    -o sqlite3_mod_fts1.$(SO) $(FTS1OBJ) $(LMSVCRT) \
	    -lgdi32 -lcomdlg32 \
	    -ladvapi32 -lshell32 -luser32 -lkernel32


fts2.o:	$(TOP)/ext/fts2/fts2.c $(FTS2HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts2/fts2.c

fts2_hash.o:	$(TOP)/ext/fts2/fts2_hash.c $(FTS2HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts2/fts2_hash.c

fts2_porter.o:	$(TOP)/ext/fts2/fts2_porter.c $(FTS2HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts2/fts2_porter.c

fts2_tokenizer1.o:	$(TOP)/ext/fts2/fts2_tokenizer1.c $(FTS2HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts2/fts2_tokenizer1.c

sqlite3_mod_fts2.$(SO):	$(FTS2OBJ)
	$(TCCX) -shared -Wl,--kill-at \
	    -Wl,-out-implib,libsqlite3fts2.a -Wl,--strip-all \
	    -o sqlite3_mod_fts2.$(SO) $(FTS2OBJ) $(LMSVCRT) \
	    -lgdi32 -lcomdlg32 \
	    -ladvapi32 -lshell32 -luser32 -lkernel32


fts3.o:	$(TOP)/ext/fts3/fts3.c $(FTS3HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts3/fts3.c

fts3_hash.o:	$(TOP)/ext/fts3/fts3_hash.c $(FTS3HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts3/fts3_hash.c

fts3_porter.o:	$(TOP)/ext/fts2/fts2_porter.c $(FTS3HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts3/fts3_porter.c

fts3_tokenizer.o:	$(TOP)/ext/fts3/fts3_tokenizer.c $(FTS3HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts3/fts3_tokenizer.c

fts3_tokenizer1.o:	$(TOP)/ext/fts3/fts3_tokenizer1.c $(FTS3HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts3/fts3_tokenizer1.c

fts3_expr.o:	$(TOP)/ext/fts3/fts3_expr.c $(FTS3HDR) $(HDR)	
	$(TCCX) -c $(TOP)/ext/fts3/fts3_expr.c

fts3_icu.o:	$(TOP)/ext/fts3/fts3_icu.c $(FTS3HDR) $(HDR)	
	$(TCCX) -c $(TOP)/ext/fts3/fts3_icu.c

fts3_write.o:	$(TOP)/ext/fts3/fts3_write.c $(FTS3HDR) $(HDR)	
	$(TCCX) -c $(TOP)/ext/fts3/fts3_write.c

fts3_snippet.o:	$(TOP)/ext/fts3/fts3_snippet.c $(FTS3HDR) $(HDR)	
	$(TCCX) -c $(TOP)/ext/fts3/fts3_snippet.c

fts3_aux.o:	$(TOP)/ext/fts3/fts3_aux.c $(FTS3HDR) $(HDR)	
	$(TCCX) -c $(TOP)/ext/fts3/fts3_aux.c

fts3_tokenize_vtab.o:	$(TOP)/ext/fts3/fts3_tokenize_vtab.c $(FTS3HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts3/fts3_tokenize_vtab.c

fts3_unicode.o:	$(TOP)/ext/fts3/fts3_unicode.c $(FTS3HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts3/fts3_unicode.c

fts3_unicode2.o:	$(TOP)/ext/fts3/fts3_unicode2.c $(FTS3HDR) $(HDR)
	$(TCCX) -c $(TOP)/ext/fts3/fts3_unicode2.c

sqlite3_mod_fts3.$(SO):	$(FTS3OBJ)
	$(TCCX) -shared -Wl,--kill-at \
	    -Wl,-out-implib,libsqlite3fts3.a -Wl,--strip-all \
	    -o sqlite3_mod_fts3.$(SO) $(FTS3OBJ) $(LMSVCRT) \
	    -lgdi32 -lcomdlg32 \
	    -ladvapi32 -lshell32 -luser32 -lkernel32


clean:	
	rm -f $(FTS1OBJ) libsqlite3fts1.a sqlite3_mod_fts1.$(SO)
	rm -f $(FTS2OBJ) libsqlite3fts2.a sqlite3_mod_fts2.$(SO)
	rm -f $(FTS3OBJ) libsqlite3fts3.a sqlite3_mod_fts3.$(SO)
